//Sabadell Survival Analysis


drop if Hospital_Death ==1


recode TTD (min/1825.1=1), gen(event2)
replace event2 = 0 if event2 ==.
replace event2 = 0 if event2 >1825.1
recode TTRD (1825.1/max=1825), gen(TTE)
replace TTE =TTD if TTE==.
stset TTE, failure (event2==1)

// Cox-regression

stcox i.Sabadell
estat phtest, detail
stcox i.age_group
estat phtest, detail
stcox ib2.Gender
estat phtest, detail
stcox i.Type
estat phtest, detail
stcox ib2.Med_Surg
estat phtest, detail
stcox LSOA_score
estat phtest, detail
stcox APACHE_II
estat phtest, detail
stcox ib2.Readmission_spell
estat phtest, detail
stcox Hosp_LOS
estat phtest, detail
stcox i.Year_admit
estat phtest, detail

stcox i.Sabadell ib2.Gender i.Type ib2.Med_Surg LSOA_score i.age_group APACHE_II ib2.Readmission_spell Hosp_LOS i.Year_admit
estat phtest, detail


// Flexible parametric survvial model

stpm2 i.Sabadell, df(9) scale(hazard) eform
estat ic
stpm2 i.Sabadell, df(8) scale(hazard) eform
estat ic
stpm2 i.Sabadell, df(7) scale(hazard) eform
estat ic
stpm2 i.Sabadell, df(6) scale(hazard) eform
estat ic
stpm2 i.Sabadell, df(5) scale(hazard) eform
estat ic
stpm2 i.Sabadell, df(4) scale(hazard) eform
estat ic
stpm2 i.Sabadell, df(3) scale(hazard) eform
estat ic

// lowest AIC/BIC numbers for both Sabadell and i.i.age_group_group are when df=3 therefore used for the rest of the model

stpm2 i.age_group, df(3) scale(hazard) eform
stpm2 ib2.Gender, df(3) scale(hazard) eform
stpm2 i.Type, df(3) scale(hazard) eform
stpm2 ib2.Med_Surg, df(3) scale(hazard) eform
stpm2 LSOA_score, df(3) scale(hazard) eform
stpm2 APACHE_II, df(3) scale(hazard) eform
stpm2 ib2.Readmission_spell, df(3) scale(hazard) eform
stpm2 Hosp_LOS, df(3) scale(hazard) eform
stpm2 i.Year_admit, df(3) scale(hazard) eform

stpm2 i.Sabadell ib2.Gender i.Type ib2.Med_Surg LSOA_score i.age_group APACHE_II ib2.Readmission_spell Hosp_LOS i.Year_admit, df(9) scale(hazard) eform
estat ic
stpm2 i.Sabadell ib2.Gender i.Type ib2.Med_Surg LSOA_score i.age_group APACHE_II ib2.Readmission_spell Hosp_LOS i.Year_admit, df(8) scale(hazard) eform
estat ic
stpm2 i.Sabadell ib2.Gender i.Type ib2.Med_Surg LSOA_score i.age_group APACHE_II ib2.Readmission_spell Hosp_LOS i.Year_admit, df(7) scale(hazard) eform
estat ic
stpm2 i.Sabadell ib2.Gender i.Type ib2.Med_Surg LSOA_score i.age_group APACHE_II ib2.Readmission_spell Hosp_LOS i.Year_admit, df(6) scale(hazard) eform
estat ic
stpm2 i.Sabadell ib2.Gender i.Type ib2.Med_Surg LSOA_score i.age_group APACHE_II ib2.Readmission_spell Hosp_LOS i.Year_admit, df(5) scale(hazard) eform
estat ic
stpm2 i.Sabadell ib2.Gender i.Type ib2.Med_Surg LSOA_score i.age_group APACHE_II ib2.Readmission_spell Hosp_LOS i.Year_admit, df(4) scale(hazard) eform
estat ic
stpm2 i.Sabadell ib2.Gender i.Type ib2.Med_Surg LSOA_score i.age_group APACHE_II ib2.Readmission_spell Hosp_LOS i.Year_admit, df(3) scale(hazard) eform
estat ic

// Again lowest AIC/BIC numbers when df=3 therefore used in the paper.

// Predict morality based on this model

predict xb, xb
predict s, survival
predict h, hazard

foreach var in Gender Type Med_Surg LSOA_score APACHE_II Readmission_spell Hosp_LOS Year_admit {
  summ `var', meanonly
  local atopt `atopt' `var' `r(mean)'
   }

range timevar 0 1826.25  61

predict s0_covave, at(Sabadell 0 age_group 1 `atopt') surv ci timevar(timevar)
predict s1_covave, at(Sabadell 0 age_group 2 `atopt') surv ci timevar(timevar)
predict s2_covave, at(Sabadell 0 age_group 3 `atopt') surv ci timevar(timevar)
predict s3_covave, at(Sabadell 0 age_group 4 `atopt') surv ci timevar(timevar)


predict s4_covave, at(Sabadell 1 age_group 1 `atopt') surv ci timevar(timevar)
predict s5_covave, at(Sabadell 1 age_group 2 `atopt') surv ci timevar(timevar)
predict s6_covave, at(Sabadell 1 age_group 3 `atopt') surv ci timevar(timevar)
predict s7_covave, at(Sabadell 1 age_group 4 `atopt') surv ci timevar(timevar)


predict s8_covave, at(Sabadell 2 age_group 1 `atopt') surv ci timevar(timevar)
predict s9_covave, at(Sabadell 2 age_group 2 `atopt') surv ci timevar(timevar)
predict s10_covave, at(Sabadell 2 age_group 3 `atopt') surv ci timevar(timevar)
predict s11_covave, at(Sabadell 2 age_group 4 `atopt') surv ci timevar(timevar)


predict s12_covave, at(Sabadell 3 age_group 1 `atopt') surv ci timevar(timevar)
predict s13_covave, at(Sabadell 3 age_group 2 `atopt') surv ci timevar(timevar)
predict s14_covave, at(Sabadell 3 age_group 3 `atopt') surv ci timevar(timevar)
predict s15_covave, at(Sabadell 3 age_group 4 `atopt') surv ci timevar(timevar)



